﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TradeForm
{
    class NumToEN
    {
private string  SpellNumber(string MyNumber)
{
    string Dollars="", Cents="", Temp="";
    int  DecimalPlace=0, Count=0;
    string[] Place=new string[9];
    Place[2] = " Thousand ";
    Place[3] = " Million ";
    Place[4] = " Billion ";
    Place[5] = " Trillion ";
   // ' String representation of amount.
    MyNumber = MyNumber.Trim();
  //  ' Position of decimal place 0 if none.
    DecimalPlace = MyNumber.IndexOf( ".");
   // ' Convert cents and set MyNumber to dollar amount.
    if (DecimalPlace > 0 )
    {
        Cents = GetTens(Left(MyNumber.Substring ( DecimalPlace + 1)"00", 2))
        MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
    }
    Count = 1
    Do While MyNumber <> ""
        Temp = GetHundreds(Right(MyNumber, 3))
        If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
        If Len(MyNumber) > 3 Then
            MyNumber = Left(MyNumber, Len(MyNumber) - 3)
        Else
            MyNumber = ""
        End If
        Count = Count + 1
    Loop
    Select case Dollars
        case ""
            Dollars = "No Dollars"
        case "One"
            Dollars = "One Dollar"
         case Else
            Dollars = Dollars & " Dollars"
    End Select
    Select case Cents
        case ""
            Cents = " and No Cents"
        case "One"
            Cents = " and One Cent"
              case Else
            Cents = " and " & Cents & " Cents"
    End Select
    SpellNumber = Dollars & Cents
End Function
      
' Converts a number from 100-999 into text 
Function GetHundreds(ByVal MyNumber)
    Dim Result As String
    If Val(MyNumber) = 0 Then Exit Function
    MyNumber = Right("000" & MyNumber, 3)
    ' Convert the hundreds place.
    If Mid(MyNumber, 1, 1) <> "0" Then
        Result = GetDigit(Mid(MyNumber, 1, 1)) & " Hundred "
    End If
    ' Convert the tens and ones place.
    If Mid(MyNumber, 2, 1) <> "0" Then
        Result = Result & GetTens(Mid(MyNumber, 2))
    Else
        Result = Result & GetDigit(Mid(MyNumber, 3))
    End If
    GetHundreds = Result
End Function
      
//' Converts a number from 10 to 99 into text. 
private string  GetTens(string TensText)
    {
    string  Result=string.Empty;

    // ' Null out the temporary function value.

    if ((Left(TensText, 1)) == 1) 
    {// If value between 10-19...
        switch (TensText)
        {
            case 10: Result = "Ten" ;break ;
            case 11: Result = "Eleven" ;break ;
            case 12: Result = "Twelve" ;break ;
            case 13: Result = "Thirteen" ;break ;
            case 14: Result = "Fourteen" ;break ;
            case 15: Result = "Fifteen" ;break ;
            case 16: Result = "Sixteen" ;break ;
            case 17: Result = "Seventeen" ;break ;
            case 18: Result = "Eighteen" ;break ;
            case 19: Result = "Nineteen" ;break ;
            default :Result ="";break ;
    }
    }
    else {                                 // If value between 20-99...
        Select case Val(Left(TensText, 1))
            case 2: Result = "Twenty ";break ;
            case 3: Result = "Thirty ";break ;
            case 4: Result = "Forty ";break ;
            case 5: Result = "Fifty ";break ;
            case 6: Result = "Sixty ";break ;
            case 7: Result = "Seventy ";break ;
            case 8: Result = "Eighty ";break ;
            case 9: Result = "Ninety ";break ;
             default :Result ="";break ;
    }
        Result = Result & GetDigit _
            (Right(TensText, 1))  ' Retrieve ones place.
    End If
    GetTens = Result
}
     
// Converts a number from 1 to 9 into text. 
static private string   GetDigit(string  Digit)
{
    string GetDigit="";
    switch(Digit)
    {
        case 1: GetDigit = "One" ;break ;
        case 2: GetDigit = "Two";break ;
        case 3: GetDigit = "Three";break ;
        case 4: GetDigit = "Four";break ;
        case 5: GetDigit = "Five";break ;
        case 6: GetDigit = "Six";break ;
        case 7: GetDigit = "Seven";break ;
        case 8: GetDigit = "Eight";break ;
        case 9: GetDigit = "Nine";break ;
        default : GetDigit = "";break ;
}
    return GetDigit;
}
		
    #region Left
    static private string Left(string str, int n)
    {
        return str.Substring(0, n);
    }
    #endregion

    #region Right
    static private string Right(string str, int n)
    {
        return str.Substring(str.Length - n, n);
    }
    #endregion

    #region Len
    static private int Len(string str)
    {
        return str.Length;
    }
    #endregion
}
